package practice2;

import java.util.Scanner;

public class P8601 {
    
    private static int m;   //行数
    private static int n;   //列数
    private static int[][] chs;     //表示棋盘，从1开始
    private static boolean[][] isGo;    //表示该地是否走过
    private static int target;      //棋盘上数总和的一半
    private static int min = Integer.MAX_VALUE;         //输出结果,最小的格子数
    private static int xarr[] = {0,1,0,-1};    //走法
    private static int yarr[] = {1,0,-1,0};
    
    private static void dfs(int x,int y,int step,int sum){      //xy为当前横纵坐标,step为当前格子数,sum为当前总和
        if(sum == target){      //达到目标要求
            min = Math.min(min,step);
        }
        for(int i=0;i<4;i++){
            int dx = x + xarr[i];
            int dy = y + yarr[i];    
            if(dx>0 && dx<=m && dy>0 && dy<=n && !isGo[dx][dy]){      //剪枝: 不出界且没走过
                isGo[dx][dy] = true;
                dfs(dx,dy,step+1,sum+chs[dx][dy]);
                isGo[dx][dy] = false;     //回溯
            }
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n =sc.nextInt();        //n列
        m =sc.nextInt();        //m行
        chs = new int[m+1][n+1];
        isGo =new boolean[m+1][n+1];
        int total = 0;
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                chs[i][j] = sc.nextInt();
                isGo[i][j] = false;
                total += chs[i][j];
            }
        }
        target = total/2;
        dfs(1,1,1,chs[1][1]);
        if(min == Integer.MAX_VALUE){
            System.out.println(0);
        }else {
            System.out.println(min);
        }
    }
}
